package com.zzx.bms.controller;

import com.zzx.bms.core.util.Page;
import com.zzx.bms.domain.Department;
import com.zzx.bms.domain.Role;
import com.zzx.bms.domain.Role;
import com.zzx.bms.domain.User;
import com.zzx.bms.service.IDepartmentService;
import com.zzx.bms.service.IRoleService;
import com.zzx.bms.service.IRoleService;
import com.zzx.bms.service.IUserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SamZhao on 2016/12/6.
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    private static Logger logger = Logger.getLogger(RoleController.class);

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    public String roleList(Role role, Model model) throws Exception {

        List<Role> rollist = roleService.findAllRoleList();
        model.addAttribute("rollist", rollist);

        return "role/roleList";
    }

    @RequestMapping(value = "/addRole")
    public String addRole(Model model){
        return "role/addRole";
    }

    @RequestMapping("/saveRole")
    public @ResponseBody String saveRole(Role role) throws Exception {
        int flag = roleService.saveRole(role);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping("/deleteRole")
    public @ResponseBody String deleteRole(Role role, String id) throws Exception {
        int flag = roleService.deleteRoleById(id);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping(value = "/checkRole")
    public @ResponseBody String checkRole(Model model, Role role){
        int flag = roleService.checkRole(role);
        if(flag > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping(value = "/editRole")
    public String editRole(Model model, String id){
        Role role = roleService.findRoleById(id);
        model.addAttribute("role", role);
        return "role/editRole";
    }
}
